package com.dianxun.exam.ui.activity;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dianxun.exam.MainApplication;
import com.dianxun.exam.R;
import com.dianxun.exam.adapter.ExamFinishAdapter;
import com.dianxun.exam.common.AppConfig;
import com.dianxun.exam.common.AppKeyManager;
import com.dianxun.exam.common.base.BaseActivity;
import com.dianxun.exam.entity.AnswerBean;
import com.dianxun.exam.entity.AnswerBeanDao;
import com.dianxun.exam.entity.AnswerJudgmentListBean;
import com.dianxun.exam.entity.ChoiceListBean;
import com.dianxun.exam.entity.DaoSession;
import com.dianxun.exam.entity.JudgmentListBean;
import com.dianxun.exam.entity.TikuBean;
import com.dianxun.exam.entity.TikuBeanDao;
import com.dianxun.exam.entity.TitleEntity;
import com.dianxun.exam.entity.TypeEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 考试结果
 */
public class ExamFinishActivity extends BaseActivity implements View.OnClickListener {

    public static final String EXTRA_TIKUID = "tikuid";
    @BindView(R.id.tv_back)
    TextView tv_back;
    @BindView(R.id.list_paper)
    RecyclerView list_paper;
    private ExamFinishAdapter adapter;
    private DaoSession daoSession;
    private TikuBeanDao tikuBeanDao;
    private AnswerBeanDao answerBeanDao;
    private TikuBean tikuBean;

    @Override
    protected int getContentView() {
        return R.layout.activity_exam_finish;
    }

    @Override
    protected void initView() {
        daoSession = ((MainApplication) getApplication()).getDaoSession();
        answerBeanDao = daoSession.getAnswerBeanDao();
        tikuBeanDao = daoSession.getTikuBeanDao();
        List<TikuBean> tikuBeanList = tikuBeanDao.queryBuilder()
                .where(TikuBeanDao.Properties.TikuId.eq(getIntent().getExtras().getString(EXTRA_TIKUID)))
                .list();
        tikuBean = tikuBeanList.get(0);

        list_paper.setLayoutManager(new LinearLayoutManager(self));
        list_paper.setNestedScrollingEnabled(false);
        list_paper.setItemViewCacheSize(1000);

        List<AnswerBean> answerBeans = answerBeanDao.queryBuilder()
                .where(AnswerBeanDao.Properties.TikuId.eq(tikuBean.getTikuId()),
                        AnswerBeanDao.Properties.UserId.eq(AppConfig.getInstance().getUserId()))
                .list();
        List<TypeEntity> datas = new ArrayList<>();
        AnswerBean answerBean = answerBeans.get(0);

        if (tikuBean.getJudgmentList().size() > 0) {
            TitleEntity judgmentTitle = new TitleEntity("判断题（红色为正确答案）");
            judgmentTitle.setItemType(AppKeyManager.TYPE_TITLE);
            datas.add(judgmentTitle);

            for (int i = 0; i < tikuBean.getJudgmentList().size(); i++) {
                JudgmentListBean judgmentListBean = tikuBean.getJudgmentList().get(i);
                judgmentListBean.setItemType(AppKeyManager.TYPE_JUDGMENT);
                AnswerJudgmentListBean answerJudgmentListBean = answerBean.getJudgmentList().get(i);
                judgmentListBean.setAnswer(answerJudgmentListBean.getAnswer());
                datas.add(judgmentListBean);
            }
        }

        if (tikuBean.getChoice0List().size() > 0) {
            TitleEntity singleChoiceTitle = new TitleEntity("单选题（红色为正确答案）");
            singleChoiceTitle.setItemType(AppKeyManager.TYPE_TITLE);
            datas.add(singleChoiceTitle);

            for (int i = 0; i < tikuBean.getChoice0List().size(); i++) {
                ChoiceListBean choiceListBean = tikuBean.getChoice0List().get(i);
                choiceListBean.setItemType(AppKeyManager.TYPE_SINGLE_CHOICE);
                for (int j = 0; j < choiceListBean.getItemList().size(); j++) {
                    choiceListBean.getItemList().get(j).setAnswer(answerBean.getChoiceList().get(i).getItemList().get(j).getAnswer());
                }
                datas.add(choiceListBean);
            }
        }
        if (tikuBean.getChoice1List().size() > 0) {
            TitleEntity multipleChoiceTitle = new TitleEntity("多选题（红色为正确答案）");
            multipleChoiceTitle.setItemType(AppKeyManager.TYPE_TITLE);
            datas.add(multipleChoiceTitle);

            for (int i = 0; i < tikuBean.getChoice1List().size(); i++) {
                ChoiceListBean choiceListBean = tikuBean.getChoice1List().get(i);
                choiceListBean.setItemType(AppKeyManager.TYPE_MULTIPLE_CHOICE);
                for (int j = 0; j < choiceListBean.getItemList().size(); j++) {
                    choiceListBean.getItemList().get(j).setAnswer(answerBean.getChoiceList()
                            .get(i + tikuBean.getChoice0List().size()).getItemList().get(j).getAnswer());
                }
                datas.add(choiceListBean);
            }
        }
        adapter = new ExamFinishAdapter(datas);
        adapter.setHasStableIds(true);

        View headerView = getLayoutInflater().inflate(R.layout.item_exam_finish_header, (ViewGroup) list_paper.getParent(), false);
        TextView tv_paper_no = headerView.findViewById(R.id.tv_paper_no);
        TextView tv_paper_name = headerView.findViewById(R.id.tv_paper_name);
        TextView tv_time = headerView.findViewById(R.id.tv_time);
        TextView tv_score = headerView.findViewById(R.id.tv_score);
        TextView tv_examinee_name = headerView.findViewById(R.id.tv_examinee_name);
        TextView tv_get_source = headerView.findViewById(R.id.tv_get_source);
        adapter.addHeaderView(headerView);
        tv_paper_no.setText(tikuBean.getLotNo());
        tv_paper_name.setText(tikuBean.getName());
        tv_time.setText(tikuBean.getTotalTime() + "分钟");
        tv_score.setText(tikuBean.getTotalScore() + "分");
        tv_examinee_name.setText(AppConfig.getInstance().getDisplayName());

        List<JudgmentListBean> judgmentAnswers = tikuBean.getJudgmentList();
        int judgmentSource = 0;
        for (int i = 0; i < judgmentAnswers.size(); i++) {
            JudgmentListBean judgmentListBean = judgmentAnswers.get(i);
            if (judgmentListBean.getItemAnswer().equals(Integer.toString(judgmentListBean.getAnswer()))) {
                judgmentSource += judgmentListBean.getScore();
            }
        }
        List<ChoiceListBean> choice0Answers = tikuBean.getChoice0List();
        int choice0Source = 0;
        for (int i = 0; i < choice0Answers.size(); i++) {
            ChoiceListBean choiceListBean0 = choice0Answers.get(i);
            boolean failed = false;
            for (int j = 0; j < choiceListBean0.getItemList().size(); j++) {
                if (!choiceListBean0.getItemList().get(j).getItemAnswer().equals(Integer.toString(choiceListBean0.getItemList().get(j).getAnswer()))) {
                    failed = true;
                    break;
                }
            }
            if (!failed) {
                choice0Source += choiceListBean0.getScore();
            }
        }
        List<ChoiceListBean> choice1Answers = tikuBean.getChoice1List();
        int choice1Source = 0;
        for (int i = 0; i < choice1Answers.size(); i++) {
            ChoiceListBean choiceListBean1 = choice1Answers.get(i);
            boolean failed = false;
            for (int j = 0; j < choiceListBean1.getItemList().size(); j++) {
                if (!choiceListBean1.getItemList().get(j).getItemAnswer().equals(Integer.toString(choiceListBean1.getItemList().get(j).getAnswer()))) {
                    failed = true;
                    break;
                }
            }
            if (!failed) {
                choice1Source += choiceListBean1.getScore();
            }
        }

        tv_get_source.setText((judgmentSource + choice0Source + choice1Source) + "分");
        list_paper.setAdapter(adapter);
    }

    @Override
    protected void initListeners() {
        tv_back.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_back:
                finish();
                break;
        }
    }
}
